
package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class WineRegionManagement {
    
    def listWineRegions() {
        def wr = WineRegion.list()
        JSONArray arr = new JSONArray()
        wr.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("wineRegionName",it.wineRegionName)
            obj.put("description",it.description)
            obj.put("countryId",it.countryId)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getWineRegionByID(id) {
        def wr = WineRegion.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",wr.id)
        obj.put("wineRegionName",wr.wineRegionName)
        obj.put("description",wr.description)
        obj.put("countryId",wr.countryId)
        
        return obj
    }
    
//    def getWineRegionByName(name) {
//        def WineRegion = WineRegion.findBywineRegionName(name)
//        JSONObject obj = new JSONObject()
//        obj.put("id",it.id)
//        obj.put("wineRegionName",it.wineRegionName)
//        obj.put("description",it.description)
//        
//        return obj
//    }

    def getWineRegionForDbByWineRegionName(wineRegionName) {
        def wr = WineRegion.findByWineRegionName(wineRegionName)
        
        return wr
    }    
    
    def addWineRegion(o) {
        def status
        
        WineRegion wr = new WineRegion()
        wr.wineRegionName = o.get("wineRegionName")
        if (o.get("description") == "") {
            wr.description = "-"
        }
        else {
            wr.description = o.get("description")
        }
        
        CountryManagement cman = new CountryManagement()
        def cty = cman.getCountryForDbByCountryName(o.get("country"));
        if (cty != null) {
//            wr.countryId = cty.id 
            wr.country = cty
        }
        if (wr.save()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add Wine >> ' + status
        
        return status
    }
    
    def updateWineRegion(wineRegion) {
        def status
        
        WineRegion wr = WineRegion.findById(wineRegion.id)
        if (wineRegion.has("wineRegionName")) { wr.wineRegionName = wineRegion.wineRegionName }
        if (wineRegion.has("description")) { wr.description = wineRegion.description }
        if (wineRegion.has("countryId")) { wr.countryId = wineRegion.countryId }
        if (wr.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update Wine >> ' + status
        
        return status
    }
    
    def deleteWineRegion(id) {
        def status
        
        WineRegion wr = WineRegion.findById(id)
        if (wr.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete Wine >> ' + status
        
        return status
    }
}

